package com.edu.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.edu.bean.Book;
import com.edu.service.BookService;
import com.edu.service.UserService;
import com.edu.service.impl.BookServiceImpl;
import com.edu.service.impl.UserServiceImpl;


@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserService us = new UserServiceImpl();
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//保存账号密码
		String autoSave = request.getParameter("autoSave");
		if("on".equals(autoSave)){
			Cookie cookie = new Cookie("uname",username);
			Cookie cookie2 = new Cookie("upwd",password);
			cookie.setMaxAge(60*60*24*7);
			cookie2.setMaxAge(60*60*24*7);
			response.addCookie(cookie);
			response.addCookie(cookie2);
		}else{
			Cookie cookie = new Cookie("uname","");
			Cookie cookie2 = new Cookie("upwd","");
			cookie.setMaxAge(0);
			cookie2.setMaxAge(0);
			response.addCookie(cookie);
			response.addCookie(cookie2);
		}
		
		Map<String,Object> map = us.userLogin(username, password);
		if(map.get("loginUser")!=null){
			session.setAttribute("loginUser", map.get("loginUser"));
			response.sendRedirect("book.jsp");
		}else{
			request.setAttribute("msg",map.get("msg"));
			request.getRequestDispatcher("login.jsp").forward(request,response);
		}
		
	}

}
